package com.ddone.config;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @author ddone
 * @date 2024/4/5-16:22
 */

@Configuration
@Slf4j
public class RabbitMqConsumerConfig {

    @RabbitListener(queues = "order.queue")
    public void consumerOder(String msg) {
        log.error("消费消息");
        System.out.println("获取到的消息: " + msg);
    }


    // 创建新的交换机和队列
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "order.queue.dead", durable = "true"),
                    // 创建新队列
                    exchange = @Exchange(value = "order.exchange.dead", durable = "true",
                            ignoreDeclarationExceptions = "true", type = ExchangeTypes.TOPIC
                    ),
                    // 创建交换机
                    key = "a.*"
            )
    )
    public void consumerOrder2(String msg, Channel channel, Message message) throws IOException {
        try {
            int i = 2/0;
            log.warn("消费消息");
            System.out.println("consumer2获取到的消息: "+msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            if (!message.getMessageProperties().getRedelivered()){
                // 消息已经重新投递过
                System.out.println("消息重试后依然失败，拒绝再次接收");
                // 拒绝消息，不再重新入队（如果绑定了死信队列消息会进入死信队列，没有绑定死信队列则消息被丢弃，也可以把失败消息记录到redis或者mysql中），也可以设置为true再重试。
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            } else {
                System.out.println("消息消费时出现异常，即将再次返回队列处理");
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            }
            e.printStackTrace();

        }
    }

    @RabbitListener(queues = "order.queue.biz")
    public void consumerTestDead(String msg, Channel channel, Message message) throws IOException {
        try {
            int i = 2/0;
            log.warn("消费消息");
            System.out.println("consumer2获取到的消息: "+msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()){
                // 消息已经重新投递过
                System.out.println("消息重试后依然失败，拒绝再次接收");
                // 拒绝消息，不再重新入队（如果绑定了死信队列消息会进入死信队列，没有绑定死信队列则消息被丢弃，也可以把失败消息记录到redis或者mysql中），也可以设置为true再重试。
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            } else {
                System.out.println("消息消费时出现异常，即将再次返回队列处理");
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            }
            e.printStackTrace();

        }
    }

}
